import ProTable from '@ant-design/pro-table';
import React, {useRef, useState} from 'react';
import {useMount, useRequest} from 'ahooks';
import {editInvitation, findByAgentBySelectList, findByPageInvitationList,} from '@/services/ant-design-pro/agent';
import {
  ProForm,
  ProFormCheckbox,
  ProFormInstance,
  ProFormMoney,
  ProFormSelect, ProFormSwitch,
  ProFormText,

} from '@ant-design/pro-form';
import ProCard from '@ant-design/pro-card';
import {findByGoodsBySelectList} from '@/services/ant-design-pro/goods';
import {Button, message, Switch, Tag} from 'antd';
import {PlusOutlined} from '@ant-design/icons';
import {getResult} from "@/services/ant-design-pro/attribute";

const getAttr = async (key: string) => {
  let result = await getResult ( key, 0 );
  console.log ("getAttr----------------------",result )
  return result
}
const columns = [
  {
    title: '编号',
    dataIndex: 'index',
    key: 'index',
    valueType: 'indexBorder',
  },
  {
    title: '推荐人等级',
    dataIndex: 'agentName',
    key: 'agentName',
    search: false,
  },
  {
    title: '下单代理等级',
    dataIndex: 'relationAgentName',
    key: 'relationAgentName',
    search: false,
  },
  {
    title: '当前的等级',
    dataIndex: 'currentUpgradeAgentName',
    key: 'currentUpgradeAgentName',
    search: false,
  },
  {
    title: '升级等级',
    dataIndex: 'upgradeAgentName',
    key: 'upgradeAgentName',
    search: false,
  },
  {
    title: '代理门槛',
    dataIndex: 'price',
    key: 'price',
    search: false,
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (row.price?row.price:0)+(row.isPrice==0?'￥':'%')
     // return row.price+(row.isPrice==0?'￥':'%')
    }
  },
  {
    title: '升级提成',
    dataIndex: 'upgradePrice',
    valueType: 'money',
    key: 'upgradePrice',
    search: false,
  },
  {
    title: '升级减',
    dataIndex: 'reduceUpgradePrice',
    valueType: 'money',
    key: 'reduceUpgradePrice',
    search: false,
  },
  {
    title: '商品上级提成',
    dataIndex: 'parentPrice',
    valueType: 'money',
    key: 'parentPrice',
    search: false,
  },
  {
    title: '一级返利',
    dataIndex: 'level1',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (row.level1?row.level1:0)+(row.isLevel==0?'￥':'%')
    },
    key: 'level1',
    search: false,
  },
  {
    title: '二级返利',
    dataIndex: 'level2',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (row.level2?row.level2:0)+(row.isLevel==0?'￥':'%')
    },
    key: 'level12',
    search: false,
  },
  {
    title: '三级返利',
    dataIndex: 'level3',
    render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => {
      return (row.level3?row.level3:0)+(row.isLevel==0?'￥':'%')
    },
    key: 'level13',
    search: false,
  },

  {
    title: '时间',
    dataIndex: 'createDate',
    valueType: 'dateTime',
    key: 'createDate',
    search: false,
    hideInTable: true,
  },
  {
    title: '商品名称',
    dataIndex: 'goodsName',
    key: 'goodsName',
    search: false
  },
  {
    title: "状态",
    key: "state",
    dataIndex: "state",
    valueType: "select",
   // valueEnum: getAttr ( "agentInvitationRelationship" ),
   valueEnum:await  getResult ( 'agentInvitationRelationship', 0 ),
    //   hideInTable: true,
  },
  {
    title: '活动',
    dataIndex: 'isActivty',
    valueType: 'switch',
    key: 'isActivty'
  }
];
const page: React.FC =  () => {
  const formRef = useRef<ProFormInstance<API.AgentInvitationRelationship>> ();
  const [params, setParams] = useState<API.AgentInvitationRelationship> ();
  const [agentInvitationRelationship, setAgentInvitationRelationship] =
    useState<API.AgentInvitationRelationship> ();

  const editDialogInfo = (row: API.AgentInvitationRelationship, index: number) => {
    setAgentInvitationRelationship ( row );
    console.log ( 'editDialogInfo', row );
    formRef?.current?.setFieldsValue ( row );
    //  formRef?.current?.setFieldsValue ( {id: row.id}  );
    console.log ( 'editDialogInfo', formRef?.current?.getFieldsValue () );
    //  setEditDialg ( true )
  };

  const {
    run: fetchByPageList,
    refresh,
    data: dataList,
    loading,
  } = useRequest ( findByPageInvitationList, {
    debounceWait: 100,
    manual: true,
  } );

  useMount ( () => {
    columns.push( {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      search: false,
      render: (_: any, row: API.AgentInvitationRelationship, index: number, action: any) => [
        <a
          key="link"
          onClick={(a) => {
            console.log ( row );
            setAgentInvitationRelationship ( row );
            editDialogInfo ( row, index );
          }}
        >
          编辑
        </a>,
      ],
    },)
    fetchByPageList ();
  } );

  // @ts-ignore
  return (
    <>
      <ProCard>
        <ProForm<API.AgentInvitationRelationship>
          formRef={formRef}
          onFinish={(values) => {
            return new Promise<Boolean> ( (resolve, reject) => {
              editInvitation ( values )
                .then ( () => {
                  message.success ( '操作成功' );
                  formRef.current?.resetFields ();
                  refresh ();
                  resolve ( true );
                } )
                .catch ( () => {
                  message.error ( '操作失败' );
                  reject ( false );
                } );
            } );
          }}
        >
          <ProFormText hidden={true} width="md" name="id"/>
          <ProForm.Group>
            <ProFormSelect
              name="agentId"
              key="agentId"
              rules={[{required: true}]}
              request={findByAgentBySelectList}
              width="sm"
              label="推荐人等级"
            />
            <ProFormSelect
              name="relationAgentId"
              key="relationAgentId"

              request={findByAgentBySelectList}
              width="sm"
              label="下单代理等级"
            />
            <ProFormSelect
              name="goodsId"
              request={findByGoodsBySelectList}
              width="sm"
              key="goodsId"
              label="产品名称"
            />
            <ProFormMoney name="price" width="sm" label="代理门槛"/>
          </ProForm.Group>
          <ProForm.Group>
            <ProFormMoney name="upgradePrice" width="sm" label="升级提成"/>
            <ProFormMoney name="level1" width="sm" label="一级返利"/>
            <ProFormMoney name="level2" width="sm" label="二级返利"/>
            <ProFormMoney name="level3" width="sm" label="三级返利"/>
          </ProForm.Group>
          <ProForm.Group>

            <ProFormSelect
              name="currentUpgradeAgentId"
              key="currentUpgradeAgentId"
              request={findByAgentBySelectList}
              width="sm"
              label="当前的等级"
            />
            <ProFormSelect
              name="upgradeAgentId"
              key="upgradeAgentId"
              request={findByAgentBySelectList}
              width="sm"
              label="升级等级"
            />
            <ProFormMoney name="parentPrice" width="sm" label="商品上级提成"/>
            <ProFormSelect
              name="state"
              key="state"
              rules={[{required: true}]}
              request={() => {
                return getResult ( "agentInvitationRelationship", 1 )
              }}
              width="sm"
              label="状态"
            />
          </ProForm.Group>
          <ProForm.Group>
            <ProFormMoney name="reduceUpgradePrice" width="sm" label="升级减"/>
            <ProFormSwitch getValueProps={((value:any) => {
              return 0;
            })} name="isActivty" width="sm" label="活动"/>
          </ProForm.Group>


        </ProForm>
      </ProCard>
      <ProTable
        columns={columns}
        dataSource={dataList?.data}
        search={{
          defaultCollapsed: false,
          labelWidth: 'auto',
        }}
        pagination={false}
        toolBarRender={() => [
          <Button
            onClick={() => {
              formRef.current?.resetFields ();
            }}
            key="button"
            icon={<PlusOutlined/>}
            type="primary"
          >
            新建
          </Button>,
        ]}
        onSubmit={(params) => {
          // fetchByPageList ( params );
          console.log ( 'onSubmit', params );
          // setParams ( {...params} )
          fetchByPageList ( params );
        }}
      ></ProTable>
    </>
  );
};

export default page;
